package com.sls.web.manager.game.service.impl;

import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.game.pojo.OhFeiGoodsAreaServerInfo;
import com.sls.web.manager.game.pojo.OhFly;
import com.sls.web.manager.game.pojo.OhFlyBrand;
import com.sls.web.manager.game.pojo.OhFlyUserInfo;
import com.sls.web.manager.game.pojo.SyncInfo;
import com.sls.web.manager.game.querybean.OhFeiGoodsAreaServerQuery;
import com.sls.web.manager.game.querybean.OhFlyBrandQuery;
import com.sls.web.manager.game.querybean.OhFlyQuery;
import com.sls.web.manager.game.service.OhFlyService;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.socket.SuperService;

@Repository("ohFlyService")
public class OhFlyServiceImpl extends SuperService implements OhFlyService {
	private static Logger log = Logger.getLogger(OhFlyServiceImpl.class);
	private Gson gson;

	public OhFlyServiceImpl() {
		gson = CommonUtil.gson();
	}

	@Override
	public Pagination<OhFly> queryOhFly(OhFlyQuery ohFlyQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter OhFlyService queryOhFly method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_PAGE_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, ohFlyQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<OhFly> ohFly = gson.fromJson(body,
					new TypeToken<Pagination<OhFly>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave OhFlyService queryOhFly method...");
			return ohFly;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public OhFly queryDetailById(String goodsId, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter OhFlyService queryDetail method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_QUERY_BY_ID);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OhFly ohFly = new OhFly();
		ohFly.setGoodsId(goodsId);
		SocketDTO dto = super.send(head, ohFly);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			OhFly ohFly2 = gson.fromJson(dto.getBody(), OhFly.class);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave OhFlyService queryDetail method...");
			return ohFly2;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public SyncInfo brandSynchronize(OhFlyUserInfo ohFlyUserInfo,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter OhFlyService brandSynchronize method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_BRAND_SYSC);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, ohFlyUserInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {

				SyncInfo result = gson.fromJson(body, SyncInfo.class);
				log.info("当前用户：" + userInfoVO.getUserId()
						+ "，Leave OhFlyService brandSynchronize method...");
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public Pagination<OhFlyBrand> queryOhFlyBrand(
			OhFlyBrandQuery ohFlyBradQuery, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter OhFlyService queryOhFlyBrand method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_BRAND_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, ohFlyBradQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<OhFlyBrand> ohFlyBrand = gson.fromJson(body,
					new TypeToken<Pagination<OhFlyBrand>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave OhFlyService queryOhFlyBrand method...");
			return ohFlyBrand;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public OhFlyUserInfo queryUerInfo(String userId, String userpws,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter queryUerInfo queryDetail method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_QUERY_BALANCE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());

		OhFlyUserInfo ohFly = new OhFlyUserInfo();
		ohFly.setUserid(userId);
		ohFly.setUserpws(userpws);
		ohFly.setVersion(Constants.OH_FLY_VERSION);
		SocketDTO dto = super.send(head, ohFly);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				OhFlyUserInfo ohFlyUerInfo = gson.fromJson(body,
						OhFlyUserInfo.class);
				if (ohFlyUerInfo != null) {
					ohFlyUerInfo.setUserid(userId);
				}
				return ohFlyUerInfo;
			} else {
				return null;
			}
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	/**
	 * 游戏区服查询
	 */
	@Override
	public Pagination<OhFeiGoodsAreaServerInfo> queryOhFlyGameArea(
			OhFeiGoodsAreaServerQuery ohFeiGoodsAreaServerQuery,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter queryOhFlyGameArea queryOhFlyGameArea method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_GAME_QUERY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, ohFeiGoodsAreaServerQuery);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			log.info("服务端返回的信息为：" + dto.getBody());
			String body = dto.getBody();
			Pagination<OhFeiGoodsAreaServerInfo> ohFlyGameArea = gson.fromJson(
					body,
					new TypeToken<Pagination<OhFeiGoodsAreaServerInfo>>() {
					}.getType());
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave queryOhFlyGameArea queryOhFlyGameArea method...");
			return ohFlyGameArea;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	/**
	 * 游戏区服信息同步
	 */
	@Override
	public int ohFlyGameAreaSync(String gameId, UserInfoVO userInfoVO)
			throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter OhFlyService ohFlyGameAreaSync method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_GOODS_AREASERVER);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OhFeiGoodsAreaServerInfo goods = new OhFeiGoodsAreaServerInfo();
		goods.setGameId(gameId);
		SocketDTO dto = super.send(head, goods);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (body == null) {
				throw new ManagerException(dto.getHead().getResponseMessage());
			}
			body = gson.fromJson(body, String.class);
			int syncCount = Integer.parseInt(body);
			log.info("当前用户：" + userInfoVO.getUserId()
					+ "，Leave OhFlyService ohFlyGameAreaSync method...");
			return syncCount;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public SyncInfo detailGoodsInfoSync(OhFlyUserInfo ohFlyUserInfo,
			UserInfoVO userInfoVO) throws Exception {
		log.info("当前用户：" + userInfoVO.getUserId()
				+ "，Enter OhFlyService detailGoodsInfoSync method...");
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_GOODS_SYSC);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head, ohFlyUserInfo);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				SyncInfo result = gson.fromJson(body, SyncInfo.class);
				log.info("当前用户：" + userInfoVO.getUserId()
						+ "，Leave OhFlyService detailGoodsInfoSync method...");
				return result;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public List<OhFlyUserInfo> queryOhFlyUserInfo(UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.QUERY_ALL_USERID_OF_OHFLY);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		SocketDTO dto = super.send(head);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
			String body = dto.getBody();
			if (StringUtils.isNotEmpty(body)) {
				List<OhFlyUserInfo> list = gson.fromJson(body,
						new TypeToken<List<OhFlyUserInfo>>() {
						}.getType());
				return list;
			}
			return null;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public boolean confirmAudit(String suppliersId, int priceRatio,
			UserInfoVO userInfoVO) throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_AUDIT);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OhFly ohfly = new OhFly();
		ohfly.setSuppliersId(suppliersId);
		ohfly.setPriceRatio(priceRatio);
		ohfly.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, ohfly);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode())) {
			return true;
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}

	@Override
	public void batchOnline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_BATCH_ON_OFF_LINE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OhFly ohfly = new OhFly();
		ohfly.setGoodsState(goodsState);
		ohfly.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, ohfly);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}

	}

	@Override
	public void batchOffline(String goodsState, UserInfoVO userInfoVO)
			throws Exception {
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.OH_FLY_BATCH_ON_OFF_LINE);
		head.setIp(userInfoVO.getCurrentIp());
		head.setUserId(userInfoVO.getUserId());
		OhFly ohfly = new OhFly();
		ohfly.setGoodsState(goodsState);
		ohfly.setOperator(userInfoVO.getUserName());
		SocketDTO dto = super.send(head, ohfly);
		if (dto != null
				&& dto.getHead() != null
				&& TransmissionCode.SUCCESS.getCode().equals(
						dto.getHead().getResponseCode())) {
		} else {
			throw new ManagerException(dto.getHead().getResponseMessage());
		}
	}
}
